/*
* CoalPulverizingSystemController.java
* Created on  202015/7/2 11:22
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/7/2       longshihui    初始版本
*
*/

package com.infitecs.eyas.mill;

import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.mill.model.PulverizingBase;
import com.infitecs.eyas.mill.model.PulverizingSection;
import com.infitecs.eyas.mill.model.PulverizingTao;
import com.infitecs.eyas.mill.service.PulverizingService;
import com.infitecs.eyas.session.SessionService;
import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/mill/CoalPulverizingSystem")
public class CoalPulverizingSystemController {

    @Autowired
    private PulverizingService pulverizingService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private ToolExpressionService toolExpressionService;

    @RequestMapping("")
    public String getPage(Model model) {
        try {
            boolean flag = pulverizingService.checkDBIsNotEmpty();
            if (!flag) {
                pulverizingService.addPulverizing();
            }
        } catch (Exception e) {
            return "mill/coal_pulverizing_system";
        }
        sessionService.setEnvironment('K');
        sessionService.putPipeCalculateValue(sessionService.getCaseId());
        model.addAttribute("baseList", pulverizingService.getRecover());
        model.addAttribute("E118",sessionService.getParamMap().get("AE118"));
        model.addAttribute("E119",sessionService.getParamMap().get("AE119"));
        return "mill/coal_pulverizing_system";
    }

    @RequestMapping(value = "/addSection", method = RequestMethod.POST)
    @ResponseBody
    public int addSection(PulverizingTao pulverizingTao) {
        return pulverizingService.addSection(pulverizingTao);
    }

    @RequestMapping(value = "/addBase", method = RequestMethod.GET)
    @ResponseBody
    public int addBase() {
        return pulverizingService.addPulverizing();
    }

    @RequestMapping(value = "/addSec", method = RequestMethod.POST)
    @ResponseBody
    public int addSec(PulverizingSection pulverizingSection) {
        return pulverizingService.addSec(pulverizingSection);
    }

    @RequestMapping(value = "/delSec", method = RequestMethod.DELETE)
    @ResponseBody
    public void delSec(@RequestParam("id") int id) {
        pulverizingService.delSec(id);
    }

    @RequestMapping(value = "/changChildTitle", method = RequestMethod.POST)
    @ResponseBody
    public void changeChildTitle(PulverizingSection pulverizingSection) {
        pulverizingService.updateChildTitle(pulverizingSection);
    }

    @RequestMapping(value = "/changeTaoTitle", method = RequestMethod.POST)
    @ResponseBody
    public void changeSectionTitle(PulverizingTao pulverizingTao) {
        pulverizingService.updateTaoTitle(pulverizingTao);
    }

    @RequestMapping(value = "/changeBaseTitle", method = RequestMethod.POST)
    @ResponseBody
    public void changeBaseTitle(PulverizingBase pulverizingBase) {
        pulverizingService.updateBaseTitle(pulverizingBase);
    }

    @RequestMapping(value = "/updateBaseParam", method = RequestMethod.POST)
    @ResponseBody
    public void updateBaseParam(@RequestBody PulverizingBase pulverizingBase) {
        pulverizingService.updateBaseParam(pulverizingBase);
    }

    @RequestMapping(value = "/updateSection", method = RequestMethod.POST)
    @ResponseBody
    public void updateSectionParam(@RequestBody PulverizingSection pulverizingSection) {
        pulverizingService.updateSection(pulverizingSection);
    }

    @RequestMapping(value = "/setE",method = RequestMethod.GET)
    @ResponseBody
    public void setE(){
        sessionService.setEnvironment('K');
    }

    @RequestMapping(value = "/getToolBarValues", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Double> getToolBarValues() {
        sessionService.setEnvironment('A');
        return toolExpressionService.getResult();
    }

    @RequestMapping(value = "/autoCalLost", method = RequestMethod.POST)
    @ResponseBody
    public Double autoCal(@RequestBody PulverizingSection pulverizingSection) {
        return pulverizingSection.getCoefficient() * pulverizingSection.getDyHead();
    }

    @RequestMapping(value = "/autoTaoLostSum",method = RequestMethod.POST)
    @ResponseBody
    public String autoTaoLostSum(@RequestBody List<PulverizingSection> pulverizingSections){
        Iterator<PulverizingSection> i = pulverizingSections.iterator();
        double sum = 0;
        PulverizingSection p;
        while (i.hasNext()){
            p = i.next();
            sum += p.getResistanceLost() == null ? 0:p.getResistanceLost();
        }
        return Double.toString(sum);
    }

    @RequestMapping(value = "/autoCalBaseLost",method = RequestMethod.POST)
    @ResponseBody
    public String autoCalBaseLost(@RequestBody List<Double> taoLostList){
        Iterator<Double> i = taoLostList.iterator();
        double sum = 0;
        while (i.hasNext()){
            sum+= i.next();
        }
        return Double.toString(sum);
    }
}
